﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using Runda.CMS.Order.Entity;
using Runda.CMS.Common.Utility;
using System.Linq.Expressions;
using System.Data.Linq.Mapping;
using System.Data.Linq;

namespace Runda.CMS.Order.Repository
{
    public class CustomerRepository
    {
        private string db = ConfigurationManager.ConnectionStrings["OrderConnectionString"].ConnectionString;

        public Object findCustomers(String customerName, int pageSize, int pageIndex, out int recordCount) {
            using (OrderDataContext context = new OrderDataContext(db))
            {
                var co = from customer in context.customer
                         where (customerName == "" ? true : customer.name.Contains(customerName))
                         select customer;
                recordCount = co.Count();
                return co.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }            
        }

        public Customer findCustomerByCustomerCode(String code)
        {
            using (OrderDataContext context = new OrderDataContext(db))
            {
                bool isExist = context.customer.Any(customer => customer.id == code);
                if (isExist)
                {
                    Customer at = context.customer.Single(customer => customer.id == code);
                    return at;
                }
                return null;
            }
        }
    }
}
